﻿// (c) Norbert Huffschmid
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.

using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using SpinDotters.Core;

namespace SpinDotters.Widgets
{
    /// <summary>
    /// View model part of Clock control
    /// </summary>
    public partial class Clock : SpinningControlContainer, INotifyPropertyChanged
    {
        /// <summary>
        /// Gets the maximum high hours value
        /// </summary>
        public int HoursHighMaximum
        {
            get { return 2; }
        }

        /// <summary>
        /// Gets the fact whether a high hour value 0 should appear as blank
        /// </summary>
        public bool HoursHighShowZeroAsBlank
        {
            get { return true; }
        }

        /// <summary>
        /// Gets the high hours part
        /// </summary>
        public int HoursHigh
        {
            get
            {
                int hour = DateTime.Now.Hour;
                if (Show12HoursFormat() == true)
                {
                    if (hour == 0)
                        return 1;
                    else if (hour <= 12)
                        return hour / 10;
                    else
                        return (hour - 12) / 10;
                }
                else
                    return hour / 10;
            }
        }

        /// <summary>
        /// Gets the low hours part
        /// </summary>
        public int HoursLow
        {
            get
            {
                int hour = DateTime.Now.Hour;
                if (Show12HoursFormat() == true)
                {
                    if (hour == 0)
                        return 2;
                    else if (hour <= 12)
                        return hour % 10;
                    else
                        return (hour - 12) % 10;
                }
                else
                    return hour % 10;
            }
        }

        /// <summary>
        /// Gets the high minutes part
        /// </summary>
        public int MinutesHigh
        {
            get { return DateTime.Now.Minute / 10; }
        }

        /// <summary>
        /// Gets the low minutes part
        /// </summary>
        public int MinutesLow
        {
            get { return DateTime.Now.Minute % 10; }
        }

        /// <summary>
        /// Gets the high seconds part
        /// </summary>
        public int SecondsHigh
        {
            get { return DateTime.Now.Second / 10; }
        }

        /// <summary>
        /// Gets the low seconds part
        /// </summary>
        public int SecondsLow
        {
            get { return DateTime.Now.Second % 10; }
        }

        /// <summary>
        /// Gets the AM/PM index
        /// </summary>
        public int AmPmIndex
        {
            get
            {
                if (DateTime.Now.Hour < 12)
                    return 0;
                else
                    return 1;
            }
        }

        /// <summary>
        /// Gets the AM/PM visibility
        /// </summary>
        public Visibility AntePostMeridiemVisibility
        {
            get
            {
                if (Show12HoursFormat() == true)
                    return System.Windows.Visibility.Visible;
                else
                    return System.Windows.Visibility.Collapsed;
            }
        }


        private bool Show12HoursFormat()
        {
            if (this.TimeFormat == ClockTimeFormat.TwelveHours)
                return true;
            else if (this.TimeFormat == ClockTimeFormat.TwentyfourHours)
                return false;
            else
            {
                DateTimeFormatInfo dtfi = CultureInfo.CurrentCulture.DateTimeFormat;
                if (dtfi.AMDesignator.Length == 0)
                    return false;
            }

            return true;
        }
    }
}
